package com.example.chuzhou.controller;


import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("portal")
@Slf4j
public class logoutController {

    @SuppressWarnings("rawtypes")
    @Resource(name = "redisStringTemplate")
    @Autowired
    private RedisTemplate redisTemplate;

    //统一认证调用登出接口
    @ApiOperation("统一认证调用登出接口")
    @PostMapping("/logout")
    public String portalLogout(HttpServletRequest request) {
        log.info("统一认证调用登出接口调用开始");
        String str = "500";
        // token信息格式为 Bearer + 空格 + access_token
        String key = request.getHeader("Authorization");

        log.info("key：" + key);
        // 根据token获取sessionId
        Object objValue = redisTemplate.opsForValue().get(key);
        log.info("/logout - objValue:" + (objValue != null));
        if (objValue != null) {
            // sessionId
            String sessionId = objValue.toString();
            log.info("/logout - sessionId:" + sessionId);
//            JSONObject data = JSONObject.parseObject(redisTemplate.opsForValue().get("ZHCS_" + sessionId) + "");
//            String Authorization = "Bearer " + data.getString("access_token");
//            log.info("/logout - Authorization:" + Authorization);
//            str = HttpRequest.httpPostData(IP + "/oauth/logout", "", Authorization);
            str = "200";
            // 清除关联信息
            redisTemplate.delete(key);
            // 清除会话
            redisTemplate.delete("ZHCS_" + sessionId);
        }
        log.info("str：" + str);
        log.info("统一认证调用登出接口调用结束");
        return str;
    }

}
